package imserver.service.impl;

import javax.annotation.Resource;

import imserver.beans.GroupInfo;
import imserver.beans.MemberInfo;
import imserver.common.util.DaoException;
import imserver.dao.BaseDaoImpl;
import imserver.service.GroupService;
import imserver.service.MemberService;

public class GroupServiceImpl extends BaseDaoImpl<GroupInfo> implements GroupService{
	
	@Resource
	private MemberService memberService;
	@Override
	public void create(GroupInfo groupInfo) throws DaoException {
		super.save(groupInfo);
	}

	@Override
	public void addMember(int id, String memberNum) throws DaoException {
		GroupInfo groupInfo = super.get(id);
		MemberInfo memberInfo = memberService.getMember(memberNum);
		groupInfo.getMembers().add(memberInfo);
		getSession().update(groupInfo);
	}

	@Override
	public void delMember(int group, int memberId) throws DaoException {
		GroupInfo groupInfo = super.get(group);
		for(int i=0; i< groupInfo.getMembers().size(); i++){
			MemberInfo memberInfo = groupInfo.getMembers().get(i);
			if(memberInfo.getId() == memberId){
				groupInfo.getMembers().remove(i);
			}
		}
		getSession().update(groupInfo);
	}

	@Override
	public void delGroup(int id) throws DaoException {
		GroupInfo group = (GroupInfo) getSession().load(GroupInfo.class, id);
		this.delete(group);
		
	}

	@Override
	public void updateName(int id, String name) throws DaoException {
		GroupInfo groupInfo = this.get(id);
		groupInfo.setName(name);
		update(groupInfo);
	}

}
